package cn.ruituo.service;

import cn.ruituo.dao.StudentDao;
import cn.ruituo.entity.Page;
import cn.ruituo.entity.Student;
import cn.ruituo.util.LogUtil;
import cn.ruituo.util.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//业务逻辑层
public class StudentService {
    public static final StudentService service=new StudentService();
    public Page<Student> paging(int pageNo, int pageSize, String sname, String qq, String telephone, Date enterDateStart, Date enterDateEnd){
        SqlSession s= MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            List<Student> list = dao.paging(pageNo, pageSize, sname, qq, telephone, enterDateStart, enterDateEnd);
            int total=dao.pageTotal(sname, qq, telephone, enterDateStart, enterDateEnd);
            return new Page<Student>(list,pageNo,total);
        } finally {
            MyBatisUtil.close(s);
        }
    }
    public Page<Student> paging(int pageNo, String sname, String qq, String telephone, Date enterDateStart, Date enterDateEnd){

        SqlSession s= MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            List<Student> list = dao.paging(pageNo, Page.PAGE_DEFAULT, sname, qq, telephone, enterDateStart, enterDateEnd);
            int total=dao.pageTotal(sname, qq, telephone, enterDateStart, enterDateEnd);
            return new Page<Student>(list,pageNo,total);
        } finally {
            MyBatisUtil.close(s);
        }
    }
    public int add(Student stu){
        SqlSession s= MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.add(stu);
        } catch (Exception e) {
            //e.printStackTrace();
            LogUtil.error("增加新生失败",e);
        } finally {
            MyBatisUtil.close(s);
        }
        return 0;
    }
    public Student get(long stuno){
        SqlSession s= MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.get(stuno);
        } finally {
            MyBatisUtil.close(s);
        }
    }
    //验证手机号是否重复
    public boolean checkPhone(String phone){
        SqlSession s= MyBatisUtil.get();
        try {
            StudentDao dao = s.getMapper(StudentDao.class);
            return dao.checkPhone(phone)!=null;
        } finally {
            MyBatisUtil.close(s);
        }
    }
}
